
<%@ page contentType="text/html;charset=UTF-8"%>
<style type="text/css">
	.modal-title{
	font-weight: bold;
	}
</style>
<div class="modal fade" id="asset_change_modal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
	  <div class="modal-dialog">
	    <div class="modal-content">
	      <div class="modal-header">
	        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
	        <h4 class="modal-title" id="exampleModalLabel">合同变更</h4>
	      </div>
	      <div class="modal-body">
	        <form id="asset-form-change" >
	      <!--   <input type="hidden" name="stateAfter">
	        <input type="hidden" name="stateBefore"> -->
	          <div class="form-group">
	          	<label for="recordDate" class="required" id="recordDate-label">变更日期</label>
	            <input name="recordDate" type="text" class="rap-input-date">
	          </div>
	          <div class="form-group quitshow">
	          	<label for="history" class="required" id="history-label">历史欠费</label>
	            <input name="history" type="text" >
	          </div>
	          <div class="form-group quitshow">
	          	<label for="deposit" class="required" id="deposit-label">租赁保证金历史交</label>
	            <input name="deposit" type="text" >
	          </div>
	          <div class="form-group">
	          	<label for="reason" class="required" id="reason-label">变更原因</label>
	            <textarea class="form-control" id="reason" name="reason"></textarea>
	          </div>
	        </form>
	      </div>
	      <div class="modal-footer">
	        <button type="button" class="btn btn-primary" id="sureModal">确定</button>
	        <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
	      </div>
	    </div>
	  </div>
	</div>

<script type="text/javascript">
var _changeType;
function changeContract(changeType){
	_changeType= changeType;
	var pks = RapDataTableUtils.getSelectPks();
	if (pks==null||pks.length < 1){
		  alert("请选择需要操作的合同！");
		  return false;
	}
	var url = "${ctx}/lease/contract/"+changeType;
	setModalShow(changeType);//设置隐藏域内容和界面显示
	popChange(url,pks,true,onRefresh);
}
/**
 * @param title:弹出框的标题
 * @param url:请求服务的url
 * @param isShowSuccess:是否alert请求成功后返回的信息（data.msg）。
 * @param fnCallback:请求成功后的回调函数名，会把服务器返回的data作为参数进行回调
 */
function popChange(url,pks,isShowSuccess,fnCallback){
	  if(isShowSuccess==null){
		  isShowSuccess=true;
	  }
	  var modal = $("#asset_change_modal");
	  modal.find('#sureModal').unbind("click");
	  modal.find('#sureModal').bind("click",function(){
		  saveChange(url,pks,isShowSuccess,fnCallback);
	  });
  	  $('#asset_change_modal').modal({
		toggle:"modal"
	 });
}
function saveChange(url,pks,isShowSuccess,fnCallback){
	  var checkResult = checkInput();
	  if(!checkResult){
		  return false;
	  }
  	  var param = $("#asset_change_modal form").serializeArray();
  	  var pkArr = {name:"pks",value:pks.toString()};
  	  param.push(pkArr);
	  $.ajax({
		"dataType" : "json",
		"type" : "POST",
		"url" : url,
		"data" : param,
		"success" : function(data) {
			$('#asset_change_modal').modal('hide');
			if (data.success) {
				if(isShowSuccess){
					if(data.msg!=null&&data.msg.length>0)
						alert(data.msg);
					else
						alert("操作成功");
				}
				if (typeof (fnCallback) != "undefined")
					fnCallback(data);
			} else {
				alert(data.msg);
			}
		},
		"error" : function(XMLHttpRequest, textStatus, errorThrown) {
			alert("操作失败，HTTP错误。");
		}
	});
}
function checkInput(){
	$("span.error").remove();
	var recordDate = $("#asset_change_modal form input[name='recordDate']").val();
	var reason =  $("#asset_change_modal form textarea[name='reason']").val();
	var flag = true;
	if(recordDate==null||recordDate.length==0){
		$("#asset_change_modal form input[name='recordDate']").after("<span class='error'>请输入变更日期</span>");
		flag=false;
	}
	if(reason==null||reason.length==0){
		$("#asset_change_modal form textarea[name='reason']").after("<span class='error'>请输入变更原因</span>");
		flag=false;
	}
	if(_changeType=="quit"){
		var history = $("#asset_change_modal form input[name='history']").val();
		var deposit =  $("#asset_change_modal form input[name='deposit']").val();
		if(history==null||history.length==0||!checkDecimal(history)){
			$("#asset_change_modal form input[name='history']").after("<span class='error'>请输入数字</span>");
			flag=false;
		}
		if(deposit==null||deposit.length==0||!checkDecimal(deposit)){
			$("#asset_change_modal form input[name='deposit']").after("<span class='error'>请输入数字</span>");
			flag=false;
		}
	}
	return flag;
}

function setModalShow(changeType){
	$("#asset_change_modal form")[0].reset();
	if(changeType=="quit"){
		$("#exampleModalLabel").text("退租确认");
		$("#recordDate-label").text("退租日期");
		$("#reason-label").text("退租原因");
		$(".quitshow").show();
	}else if(changeType=="cancel"){
		$("#exampleModalLabel").text("终止租赁合同确认");
		$("#recordDate-label").text("终止日期");
		$("#reason-label").text("终止原因");
		$(".quitshow").hide();
	}
}
</script>
